//
//  UserLoginVC.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/5/21.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "UserLoginVC.h"
#import "UserRegisterVC.h"

@interface UserLoginVC ()

@property(weak, nonatomic) IBOutlet UIView *phoneView;
@property(weak, nonatomic) IBOutlet UIView *pwdView;
@property(weak, nonatomic) IBOutlet UITextField *phoneTf;
@property(weak, nonatomic) IBOutlet UITextField *pwdTf;
@property(weak, nonatomic) IBOutlet UIButton *loginBt;
@property(weak, nonatomic) IBOutlet UIButton *regBt;
@property(weak, nonatomic) IBOutlet UIButton *forgetBt;
@property(weak, nonatomic) IBOutlet UIButton *protocolBt;

@end

@implementation UserLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
    [self removeGesture];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView {
    self.phoneView.layer.cornerRadius = 20;
    self.pwdView.layer.cornerRadius = 20;
    self.loginBt.layer.cornerRadius = 20;
    [[self.loginBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.phoneTf resignFirstResponder];
        [self.pwdTf resignFirstResponder];
        NSString *phone = self.phoneTf.text;
        NSString *pwd = self.pwdTf.text;
        if (phone.length<=0) {
            [JPToast showText:@"请输入手机号码"];
            return;
        }
        if (!isValidMobile(phone)) {
            [JPToast showText:@"请输入正确的手机号码"];
            return;
        }
        if (pwd.length<=0) {
            [JPToast showText:@"请输入密码"];
            return;
        }
        if (pwd.length<6 || pwd.length>30) {
            [JPToast showText:@"请输入正确的密码"];
            return;
        }
        [self login:phone pwd:pwd];
    }];
    [[self.regBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        UserRegisterVC *vc = [[UIStoryboard storyboardWithName:@"User" bundle:nil] instantiateViewControllerWithIdentifier:@"UserRegisterVCID"];
        [vc setType:@"注册"];
        [self.navigationController pushViewController:vc animated:NO];
    }];
    [[self.forgetBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        UserRegisterVC *vc = [[UIStoryboard storyboardWithName:@"User" bundle:nil] instantiateViewControllerWithIdentifier:@"UserRegisterVCID"];
        [vc setType:@"重置密码"];
        [self.navigationController pushViewController:vc animated:NO];
    }];
    [[self.protocolBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        UIViewController *vc = [[UIStoryboard storyboardWithName:@"User" bundle:nil] instantiateViewControllerWithIdentifier:@"UserProtocolVCID"];
        [self.navigationController pushViewController:vc animated:YES];
    }];
}

- (void)login:(NSString *)account pwd:(NSString *)pwd {
    [self showLoading:@"登录中"];
    NSDictionary *dic = @{@"phone":account, @"password":pwd};
    NSData *chipher = [JPAES random128BitAESKey];
    [[[HttpRequest shareInstance] login:dic chipher:chipher] subscribeNext:^(id x) {
        if ([x[@"code"] intValue] == 200) {
            NSString *data = [JPAES AES128DecryptWithCiphertext:x[@"data"] pwdData:chipher];
            if (data) {
                NSDictionary *dic = string2Json(data);
                [[UserManager shareInstance] setLoginMsg:dic];
                UIViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoadingVCID"];
                [self.navigationController pushViewController:vc animated:NO];
            } else {
                [JPToast showText:@"数据解析出现错误"];
            }
        } else {
            [JPToast showText:x[@"message"]];
        }
        [self hideLoading];
    } error:^(NSError *error) {
        [JPToast showText:@"网络错误"];
        [self hideLoading];
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end
